package cn.hzking.gjmall.activitys.order;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.MainActivity;
import cn.hzking.gjmall.activitys.my.MyOrderActivity;
import cn.hzking.gjmall.app.Gjmall;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.PayBean;
import cn.hzking.gjmall.bean.PayMessageBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.BaseObserver;
import cn.hzking.gjmall.utils.CheckInstallApp;
import cn.hzking.gjmall.utils.OnClickEvent;
import cn.hzking.gjmall.utils.UIUtils;
import cn.hzking.gjmall.view.SimpleOptionView;

public class PayFailActivity extends ColorStatuBarActivity {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    private String groupId;
    private IWXAPI api;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_pay_fail);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        simpleTileView.setTitle("支付失败");
        api = WXAPIFactory.createWXAPI(this, Gjmall.getWxAppId());
        Intent intent = getIntent();
        groupId = intent.getStringExtra("orderGroupId");
        simpleTileView.setLeftClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                EventBus.getDefault().post("goHome");
                Intent intent = new Intent(PayFailActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onBackPressedSupport() {
        super.onBackPressedSupport();
        EventBus.getDefault().post("goHome");
        Intent intent = new Intent(PayFailActivity.this, MainActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.btn3)
    public void restartpay(View view) {
        payWx(groupId);
    }
    @OnClick(R.id.btn4)
    public void seeorder(View view) {
        Intent intent = new Intent(this, MyOrderActivity.class);
        intent.putExtra("fragmentId",0);
        startActivity(intent);
    }


    //微信支付
    private void payWx(String orderGroupId) {
        RetrofitFactory.getInstant().API()
                .payWx(orderGroupId)
                .compose(this.<PayBean>setThread())
                .subscribe(new BaseObserver<PayBean>(this, true, true) {
                    @Override
                    protected void onSuccees(PayBean payBean) throws Exception {
                        if (payBean != null) {
                            PayBean.DataBean data = payBean.getData();
                            if (data != null) {
                                PayBean.DataBean.SignPackageBean signPackage = data.getSignPackage();
                                if (signPackage != null) {
                                    PayReq req = new PayReq();
                                    req.appId = signPackage.getAppId();
                                    req.partnerId = signPackage.getPartnerId();
                                    req.prepayId = signPackage.getPrepayid();
                                    req.packageValue = signPackage.getPackageX();
                                    req.nonceStr = signPackage.getNonceStr();
                                    req.timeStamp = signPackage.getTimeStamp();
                                    req.sign = signPackage.getPaySign();
                                    req.extData = "app data";
                                    if (CheckInstallApp.isWeixinAvilible(Gjmall.getApplicationContext())) {
                                        EventBus.getDefault().postSticky(new PayMessageBean("", "", "", "", false,String.valueOf(groupId)));
                                        api.sendReq(req);
                                        UIUtils.showToast(PayFailActivity.this, "正在调起支付", Toast.LENGTH_SHORT);
                                    } else {
                                        UIUtils.showToast(PayFailActivity.this, "请安装微信", Toast.LENGTH_SHORT);
                                    }
                                }
                            }
                        }

                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {

                    }
                });

    }
}
